Repository navigation
Normalize IPv6 addresses on read and write operations. Closes #1683 - #1735
Conversation
|
Hi @regulartim! I’ve opened a draft PR for this. I added normalize_ip() in utils.py using the standard ipaddress module (.compressed for lowercase compressed IPv6, .ipv4_mapped to convert mapped addresses back to IPv4). Since #1650 every hit goes through Hit, so I normalize src_ip there, and I also covered the API inputs (events, sensors, enrichment, Cowrie session queries) and the IOC repository lookups. Tests are added for each. Please let me know |
regulartim
left a comment
There was a problem hiding this comment.
Hey @suvani-ctrl ! Thank you, good job!
Could you please check for redundant calls of normalize_ip()? Every function that gets is data from a Hit object, does not need to call it again, right? So please check, e.g. group_valid_hits_by_ip, _bucket_key_from_hit and _normalize_raw_event_to_hit are candidates for redundant calls.
| def normalize_ip(ip_str: str) -> str: | ||
| """ | ||
| Normalizes an IP address string. | ||
| IPv4 addresses are returned unchanged, IPv4-mapped IPv6 addresses are | ||
| converted back to IPv4 and other IPv6 addresses are returned in | ||
| lower-case compressed format while invalid input is stripped. | ||
| """ | ||
| ip_str = ip_str.strip() | ||
| try: | ||
| ip = ip_address(ip_str) | ||
| except ValueError: | ||
| return ip_str | ||
|
|
||
| if ip.version == 6 and ip.ipv4_mapped: | ||
| return str(ip.ipv4_mapped) | ||
|
|
||
| return ip.compressed |
| # equivalent IP spellings must map to one canonical form everywhere downstream | ||
| src_ip = self._data.get("src_ip") | ||
| if isinstance(src_ip, str) and src_ip.strip(): | ||
| self._data["src_ip"] = normalize_ip(src_ip) |
There was a problem hiding this comment.
The class docstring says it's a read-only mapping but we're changing it here (and I think it's the right pace to do that). Please update the docstring.
267dede to
9a372f2
Compare
regulartim
left a comment
There was a problem hiding this comment.
Hey @suvani-ctrl ! Thanks for implementing the changes. Two things need to be addressed before we can merge (sorry, I did not find them last time):
- Cowrie and Tanner create IOCs from
urlparse(...).hostname. This might lead to an IPv6 inIOC.namethat is uncompressed and might cause an IntegrityError in the future. So we must also normalize IPs on the write path. I'd suggest to do this either inIocProcessor.add_iocor inIocRepository.save. That way it would cover Cowrie, Tanner andiocs_from_hitstogether. What do you think? - Please merge (or rebase on) the recent
develop. You will find a new that inBaseEnrichmentJob._match_iocsthere is a newIOC.namematching without normalization. Please fix that.
9a372f2 to
7d18d33
Compare
Hi @regulartim, thanks for the feedback! So far, the changes include: 1. Write-path IP normalization: Added 2. Enrichment query matching: Updated 3. Rebase and tests: Rebased the changes onto |
regulartim
left a comment
There was a problem hiding this comment.
Hey @suvani-ctrl ! Thanks for implementing the changes. I found another bug: _match_iocs now normalizes the keys before the query, so it returns the normalized IOC.name, right? The callers then use that name to index the original dictionary, which still has the un-normalized keys. This results in a key error. I'll add a test case for that.
A fix could be to normalize the keys where the dict is built, e.g. in _parse_feed or get_entries_by_ip. What do you think?
Hello @regulartim One thing about your test, it builds feed_by_ip & passes it straight to _match_iocs/_build_tag_entries, so it skips get_entries_by_ip. With this fix it would still fail. I’d adapt it to store the IP in a FireHolList row and build the dict via get_entries_by_ip(), so the test goes through the real code path. Is that OK with you, or would you rather keep the test as is and have the matching code handle un-normalized keys? |
|
Hi @suvani-ctrl !
That is Ok for me, please go ahead! :) |
43e67b2 to
a077007
Compare
regulartim
left a comment
There was a problem hiding this comment.
Hey @suvani-ctrl ! This looks very solid now. I am happy to merge it. Thanks for your work, well done! 👍
Thankyouu @regulartim : ) |
Description
Introduces a IP normalization utility and applies it consistently across read and write operations.
normalize_ip): Added a utility ingreedybear/utils.pythat formats native IPv6 addresses into lowercase compressed forms and safely converts IPv4-mapped IPv6 addresses (e.g.,::ffff:1.2.3.4) back to standard IPv4.Hit, bucket updaters, and raw event processing) to store IP addresses in their canonical normalized form, preventing fragmentation in analytics and trending metrics.IocRepositoryandSensorRepositoryqueries so that equivalent formatting variants correctly resolve to existing database records.Related issues
Closes #1683
Type of change
Checklist
Please complete this checklist carefully. It helps guide your contribution and lets maintainers verify that all requirements are met.
Formalities
<feature name>. Closes #999develop.develop.Docs and tests
Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.GUI changes
Ignore this section if you did not make any changes to the GUI.
Review process